package linuxbox.status.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
import java.text.*;

import linuxbox.BoxStatic;
import linuxbox.status.logic.*;
import linuxbox.status.bean.StatusKey;

/**
 * This class displays a department list.
 */
public class StatusList extends TagSupport {
  private static final String SORT_BY_LAST_NAME = "LAST_NAME";
  private static final String SORT_BY_STATUS = "STATUS";
  private static final String SORT_BY_DEPT = "DEPT";

  protected String listType = null;
  protected String sortType = SORT_BY_STATUS;
  protected String userType = BoxStatic.USER_TYPE_ALL;
  protected int selected = -1;

  public int doStartTag() throws JspException {
    try {
      StatusManager sm = StatusManager.getInstance();
      Iterator ids = null;

      if (BoxStatic.USER_TYPE_ALL.equalsIgnoreCase(userType)) {
        if (SORT_BY_STATUS.equalsIgnoreCase(sortType)) {
          ids = sm.getAllStatuses(" order by status");
        } else if (SORT_BY_LAST_NAME.equalsIgnoreCase(sortType)) {
          ids = sm.getAllStatuses(" order by lastname");
        } else if (SORT_BY_DEPT.equalsIgnoreCase(sortType)) {
          ids = sm.getAllStatuses(" order by dept_id");
        }
      } else if (BoxStatic.USER_TYPE_ONLINE.equalsIgnoreCase(userType)) {
        ids = sm.getOnlineUsers();
      } else if (BoxStatic.USER_TYPE_BUSY.equalsIgnoreCase(userType)) {
        ids = sm.getBusyUsers();
      } else if (BoxStatic.USER_TYPE_AWAY.equalsIgnoreCase(userType)) {
        ids = sm.getAwayUsers();
      } else if (BoxStatic.USER_TYPE_CHECKOUT.equalsIgnoreCase(userType)) {
        ids = sm.getCheckoutUsers();
      }

      JspWriter out = pageContext.getOut();

      StatusObject obj = null;
      StatusKey key = null;

      if (BoxStatic.DISPLAY_TYPE_TABLE.equalsIgnoreCase(listType)) {
        StringBuffer retVal = new StringBuffer();

        int i = 0;
        while (ids != null && ids.hasNext()) {
          key = (StatusKey)ids.next();
          obj = sm.getStatus(key);
          //BoxStatic.logger.debug(obj.dump());

          retVal.append("<TR");

          if ((i % 2) != 0)
            retVal.append(" bgcolor=\"#E0E0E0\">");
          else
            retVal.append(">");

          retVal.append("<TD>" + obj.getLastName() + "</TD>");
          retVal.append("<TD>" + obj.getFirstName() + "</TD>");
          retVal.append("<TD>" + sm.getStatusString(obj.getStatus()) + "</TD>");
          retVal.append("<TD>" + obj.getDeptName() + "</TD>");

          SimpleDateFormat formatter =
              new SimpleDateFormat(BoxStatic.NORMAL_DATETIME_FORMAT);
          String dateString = formatter.format(
              (Date)obj.getCheckIn());
          retVal.append("<TD>" + dateString + "</TD>");

     /*Date checkOut = (Date)obj.getCheckOut();
     if (checkOut != null) {
         dateString = formatter.format(checkOut);
         retVal.append("<TD>" + dateString + "</TD>");
     }*/

          retVal.append("</TR>");

     i++;
        }
        out.println(retVal.toString());
      }

      out.flush();
    } catch(Exception e) {
      //BoxStatic.logger.debug("Failed to get all users");
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }

  public void setListType(String aValue) {
    listType = aValue;
  }

  public void setSelected(String aValue) {
    selected = Integer.parseInt(aValue);
  }

  public void setSortType(String aStr) {
    sortType = aStr;
  }

  public void setUserType(String aStr) {
    userType = aStr;
  }
}